package com.qiku.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiku.reggie.common.R;
import com.qiku.reggie.pojo.Employee;
import com.qiku.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.cli.Digest;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    /**
     * 登录
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee , HttpSession session){
        String username = employee.getUsername();
        // 1 根据用户名密码查询
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username" , username); // 用户名条件
        Employee emp = employeeService.getOne(queryWrapper);

        if (emp == null){
            return R.error("用户名不存在");
        }

        // 2 比较密码
        String p1 = emp.getPassword();
        String p2 = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()) ; // 密码需要md5加密

        if (!p1.equals(p2)){
            return R.error("密码错误");
        }
        // 3 判断员工的状态 （1 表示启用  0 表示禁用）
        Integer status = emp.getStatus();
        if (status == 0){
            return R.error("员工状态已被禁用");
        }

        // 4 登录成功
        session.setAttribute("employee" , emp.getId());
        return R.success(emp);
    }


    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        // 销毁session
        session.invalidate();
        return R.success("推出成功");
    }



    @PostMapping()
    public R<String> add(@RequestBody Employee employee , HttpSession session){
        // 补全相关数据
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        // 设置操作时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        // 设置操作人员
//        employee.setCreateUser((Long) session.getAttribute("employee"));
//        employee.setUpdateUser((Long) session.getAttribute("employee"));

        // 调用业务层添加数据
        employeeService.save(employee);
        return R.success("添加员工成功");
    }


    /**
     * 分页条件chax
     */
    @GetMapping("/page")
    public R<Page<Employee>> page(String name , Integer page , Integer pageSize){
        // 构造分页对象
        Page<Employee> pageInfo = new Page<>(page , pageSize);
        // 构造查询条件
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        //  StringUtils.isNotBlank 去空格
        queryWrapper.like(StringUtils.isNotBlank(name) , "name" , name);
        employeeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);

    }


    /**
     * 更新员工信息
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee , HttpSession  session){
//        // 设置更新时间
//        employee.setUpdateTime(LocalDateTime.now());
//        // 设置操作人
//        employee.setUpdateUser((Long) session.getAttribute("employee"));
        // 输出当前线程的ID
        log.info("当前线程的Id=" + Thread.currentThread().getId());
        employeeService.updateById(employee);
        return R.success("更新成功");
    }


    /**
     * 根据员工id查询
     */
    @GetMapping("/{id}")
    public R<Employee> findById(@PathVariable Long id){
        Employee byId = employeeService.getById(id);
        return R.success(byId);
    }







}
